#coding=utf-8
import urllib,re,threading

def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

# 得到当前页面那些是可以继续下载 的
def getImgHtml(html):
	reg=r'http://girl-atlas.com/a/\d+'
	imgre=re.compile(reg)
	imglist = re.findall(imgre,html)
	imgset = set(imglist)
	return imgset

# 当前页面里的可以下载的tu
def getImgURL(html):
	pattern = r'http://.*?\.jpg!mid'
	imare=re.compile(pattern)
	imglist=re.findall(imare,html)
	return imglist

def getTitle(html):
	pattern = r'<title>(.*)</title>'
	tagRe = re.compile(pattern);
	return "".join(re.findall(tagRe,html)).replace("|","").replace(" ","")

class DownOnePage(threading.Thread):
	"""下载一个页面的美女图片"""
	def __init__(self, page):
		super(DownOnePage, self).__init__()
		self.page = page
		html=getHtml(page);
		self.goonPageSet=getImgHtml(html);
		self.downLoadImgList=getImgURL(html);
		self.title=getTitle(html)
		self.start()


	def run(self):
		threading.Thread.__init__(self);
		print "当前正在下载: %s \n %s 张图片 (%s):可以继续的链接(%s)"%(self.page,self.title,len(self.downLoadImgList),len(self.goonPageSet))

		for i,imgurl in enumerate(self.downLoadImgList):
			urllib.urlretrieve(imgurl, 'D:/meizitu/%s%s.jpg' %(self.title.decode('utf-8').encode('gbk'),i))
			print "downing %s %s "%(self.title,i)


def main():
	startUrl="http://girl-atlas.com/a/10141216124600000154"
	threadN = DownOnePage(startUrl)
	pageSet=threadN.goonPageSet;
	downLoadList=[]
	while len(pageSet) > 0 :
		downingImg = pageSet.pop()
		if downingImg in downLoadList : 
			continue
		threadX = DownOnePage(downingImg)
		pageSet2=threadX.goonPageSet;
		pageSet = pageSet | pageSet2
		downLoadList.append(downingImg)



main()




# http://girl-atlas.com/a/10141216124600000154
